import { deleteInit, getInit, putInit, query, request, success } from '../api'
import { asyncResponseHandler } from './_base'
import { initUserState } from './initData'
import {
  admin_add_department,
  admin_add_salary,
  admin_delete_blog, admin_delete_department,
  admin_delete_salary,
  admin_delete_user,
  admin_login,
  admin_set_all_department_list, admin_set_current_department,
  admin_set_department_info,
  admin_set_department_list,
  admin_set_user_info,
  admin_set_user_list,
  admin_set_user_salary_list, admin_update_department_info, admin_update_salary,
  rememberUserState,
  saveUserState,
  set_salary_list,
  set_user_info,
  setRegisterSuccess
} from './actions'
import { postInit } from '../api'

/**
 * 登陆
 * @param email
 * @param username
 * @param password
 * @param remember
 * @returns {function(*): Promise<void>}
 */
export const loginAsync = ({ email, username, password, remember }) =>
  async dispatch => {
    let response = await request('/api/user/login', postInit({ email, username, password }))
    let data = await asyncResponseHandler(response)
    let userState = data || initUserState
    if (remember) dispatch(rememberUserState(userState))
    else dispatch(saveUserState(userState))
  }
/**
 * 注册
 * @param username
 * @param password
 * @param email
 * @param verifyCode
 * @returns {function(*): Promise<void>}
 */
export const registerAsync = ({ username, password, email, verifyCode = 1 }) =>
  async dispatch => {
    let response = await request('/api/user/register', postInit({ email, password, username, verifyCode }))
    await asyncResponseHandler(response)
    dispatch(setRegisterSuccess(true))
  }

/**
 * 设置用户信息
 * @param userName
 * @param gender
 * @returns {function(*): Promise<void>}
 */
export const setUserInfoAsync = ({ name, age, gender, phone }) =>
  async dispatch => {
    const response = await request(`/api/user/info?${query()}`, postInit({ name, age, gender, phone }))
    await asyncResponseHandler(response)
    dispatch(set_user_info({ name, age, gender, phone }))
  }

/**
 * 获取用户信息
 * @returns {function(*): Promise<void>}
 */
export const getUserInfoAsync = () =>
  async dispatch => {
    const response = await request(`/api/user/info?${query()}`, getInit())
    let data = await asyncResponseHandler(response)
    dispatch(set_user_info(data))
  }

/**
 * 获取薪资列表
 * @returns {function(*): Promise<void>}
 */
export const getUserSalaryListAsync = () =>
  async dispatch => {
    const response = await request(`/api/user/salary?${query()}`, getInit())
    let data = await asyncResponseHandler(response)
    dispatch(set_salary_list(data || []))
  }

/**
 * 获取用户列表
 * @returns {function(*): Promise<void>}
 */
export const getAllUsersListAsync = () =>
  async dispatch => {
    const response = await request(`/api/admin/user/list`, getInit(false))
    let data = await asyncResponseHandler(response)
    dispatch(admin_set_user_list(data))
  }

/**
 * 删除指定用户
 * @param userId
 * @returns {function(*): Promise<void>}
 */
export const adminDeleteUserAsync = userId =>
  async dispatch => {
    const response = await request(`/api/admin/user?${query({ userId })}`, deleteInit({ userId }, false))
    await asyncResponseHandler(response)
    dispatch(admin_delete_user(userId))
  }

/**
 * 管理员登陆
 * @param username
 * @param password
 * @returns {function(*): Promise<void>}
 */
export const adminLoginAsync = ({ username, password }) =>
  async dispatch => {
    const response = await request('/api/admin/login', postInit({ username, password }))
    let data = await asyncResponseHandler(response)
    dispatch(admin_login(data))
  }

/**
 * 通过部门id获取部门列表
 * @param departmentId
 * @returns {function(*): Promise<void>}
 */
export const getDepartmentListAsync = departmentId =>
  async dispatch => {
    const response = await request(`/api/admin/department/list?${query({ departmentId })}`, getInit(false))
    let data = await asyncResponseHandler(response)
    dispatch(admin_set_department_list(data || []))
  }

/**
 * 获取所有的部门列表
 * @returns {function(*): Promise<void>}
 */
export const getAllDepartmentListAsync = () =>
  async dispatch => {
    const response = await request(`/api/admin/department/list?${query()}`, getInit(false))
    let data = await asyncResponseHandler(response)
    dispatch(admin_set_all_department_list(data))
  }

/**
 * 管理员通过用户id 获取该用户的薪资列表
 * @param userId
 * @returns {function(*): Promise<void>}
 */
export const getUserSalaryListByAdminAsync = userId =>
  async dispatch => {
    const response = await request(`/api/admin/salary?${query({ userId })}`, getInit(false))
    let data = await asyncResponseHandler(response)
    dispatch(admin_set_user_salary_list(data || []))
  }

/**
 * 给指定用户添加一个薪资记录
 * @param userId
 * @param postSalary
 * @param performanceSalary
 * @param workYearSalary
 * @param allowanceSalary
 * @param flag
 * @returns {function(*): Promise<void>}
 */
export const addUserSalaryAsync = ({ userId, postSalary, performanceSalary, workYearSalary, allowanceSalary, flag }) =>
  async dispatch => {
    const response = await request(`/api/admin/salary?${query({ userId })}`, postInit({
      postSalary,
      performanceSalary,
      workYearSalary,
      allowanceSalary,
      flag
    }, false))
    await asyncResponseHandler(response)
    success('添加成功')
    dispatch(admin_add_salary({
      userId,
      postSalary,
      performanceSalary,
      workYearSalary,
      allowanceSalary,
      flag,
      total: postSalary + performanceSalary + workYearSalary + allowanceSalary
    }))
  }

/**
 * 通过薪资id删掉指定用户的某一条薪资记录
 * @param salaryId
 * @returns {function(*): Promise<void>}
 */
export const deleteUserSalaryAsync = salaryId =>
  async dispatch => {
    const response = await request(`/api/admin/salary?${query({ salaryId })}`, deleteInit({}, false))
    await asyncResponseHandler(response)
    dispatch(admin_delete_salary(salaryId))
  }

/**
 * 管理员修改用户信息
 * @param userId
 * @param departmentId
 * @param password
 * @param username
 * @param email
 * @param name
 * @param age
 * @param gender
 * @param phone
 * @returns {function(*): Promise<void>}
 */
export const setUserByAdminAsync = ({ userId, departmentId, password, username, email, name, age, gender, phone }) =>
  async dispatch => {
    const response = await request(`/api/admin/user?${query({ userId })}`, postInit({
      departmentId,
      password,
      username,
      email,
      name,
      age,
      gender,
      phone
    }, false))
    await asyncResponseHandler(response)
    dispatch(admin_set_user_info({ userId, departmentId, password, username, email, name, age, gender, phone }))
  }

/**
 * 管理员添加部门
 * @param name
 * @param phone
 * @param place
 * @param parentId
 * @returns {function(*): Promise<void>}
 */
export const addDepartmentAsync = ({ name, phone, place, parentId }) =>
  async dispatch => {
    const response = await request(`/api/admin/department`, postInit({ name, phone, place, parentId }, false))
    let data = await asyncResponseHandler(response)
    dispatch(admin_add_department({ ...data, name, phone, place, parentId }))
  }

/**
 * 通过部门Id 获取部门详细信息
 * @param departmentId
 * @returns {function(*): Promise<void>}
 */
export const getDepartmentAsync = departmentId =>
  async dispatch => {
    const response = await request(`/api/admin/department?${query({ departmentId })}`, getInit(false))
    let data = await asyncResponseHandler(response)
    dispatch(admin_set_current_department(data))
  }

/**
 * 通过id删除某一个部门
 * @param departmentId
 * @returns {function(*): Promise<void>}
 */
export const deleteDepartmentAsync = departmentId =>
  async dispatch => {
    const response = await request(`/api/admin/department?${query({ departmentId })}`, deleteInit({}, false))
    await asyncResponseHandler(response)
    dispatch(admin_delete_department(departmentId))
  }
/**
 * 通过部门id更新部门信息
 * @param id
 * @param name
 * @param place
 * @param phone
 * @param parentId
 * @returns {function(*): Promise<void>}
 */
export const updateDepartmentAsync = ({ id, name, place, phone, parentId }) =>
  async dispatch => {
    const response = await request(`/api/admin/department`, putInit({ id, parentId, name, place, phone }, false))
    await asyncResponseHandler(response)
    dispatch(admin_update_department_info({ id, name, place, phone, time: new Date().toString() }))
  }

/**
 * 更新指定用户薪资信息
 * @param userId
 * @param postSalary
 * @param performanceSalary
 * @param workYearSalary
 * @param allowanceSalary
 * @param flag
 * @param salaryId
 * @returns {function(*): Promise<void>}
 */
export const updateUserSalaryAsync = ({ userId, postSalary, performanceSalary, workYearSalary, allowanceSalary, flag, salaryId }) =>
  async dispatch => {
    const response = await request(`/api/admin/salary?${query({ userId })}`, putInit({
      userId,
      postSalary,
      performanceSalary,
      workYearSalary,
      allowanceSalary,
      flag,
      salaryId
    }, false))
    await asyncResponseHandler(response)
    dispatch(admin_update_salary({
      userId,
      postSalary,
      performanceSalary,
      workYearSalary,
      allowanceSalary,
      flag,
      salaryId,
      total: postSalary + performanceSalary + workYearSalary + allowanceSalary,
      time: new Date().toString()
    }))
  }